package cn.yiyuan.controller;

import cn.yiyuan.base.annotate.Authority;
import cn.yiyuan.base.untli.AjaxResult;
import cn.yiyuan.base.untli.PageInfo;
import cn.yiyuan.domain.Dep;
import cn.yiyuan.query.DepQuery;
import cn.yiyuan.service.DepService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @user m
 * @project system_boog
 * @date 2023-10-11
 * @package cn.yiyuan.controller
 */
@RestController
@RequestMapping("/dep")

@Authority(name = "Dep")
public class DepController {
//    注入service
    @Autowired
    private DepService depService;

    @ApiOperation(value = "该方法是用于新增或修改部门")
    @PutMapping
    @Authority(name = "该方法是用于新增或修改部门")
//    新增&修改
    public AjaxResult insertOrUpdate(@RequestBody Dep dep){
        try {
            if (Objects.isNull(dep.getId())){
                depService.insert(dep);
            }else{
                depService.update(dep);
            }
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
//    删
    @Authority(name = "该方法是用于删除")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id")Long id ){
        try {
            depService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

//    查
    @Authority(name = "该方法是用于根据id查询")
    @GetMapping("/{id}")
    public AjaxResult getId(@PathVariable("id") Long id){
        try {
            return AjaxResult.success(depService.getById(id));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @Authority(name = "该方法是用于获取全部")
    @GetMapping()
    public AjaxResult getAll(){
        try {
            return AjaxResult.success(depService.getAll());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
//    前端发送的请求
//    获取列表的请求
    @Authority(name = "该方法是用于分页查询")
    @PostMapping
    public AjaxResult DepList(@RequestBody DepQuery depQuery){
        try {
            PageInfo<Dep>pageInfo = depService.querList(depQuery);
            return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

//    批量删除接口
    @Authority(name = "该方法是用于批量删除")
    @PatchMapping
public AjaxResult pachDelete(@RequestBody List<Long> ids){
    try {

        depService.pachdelete(ids);
        return AjaxResult.success();
    } catch (Exception e) {
        e.printStackTrace();
        return AjaxResult.error();
    }
}
    @Authority(name = "该方法是用于")
    @GetMapping("/parent")
    public AjaxResult getparent(){
        try {
           List<Dep> parents = depService.getParent();
           return AjaxResult.success(parents);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

}
